package com.situ.studentmanagement.controller;

import com.situ.studentmanagement.Constants;
import com.situ.studentmanagement.Utils;
import com.situ.studentmanagement.dao.AdminMapper;
import com.situ.studentmanagement.dao.StudentMapper;
import com.situ.studentmanagement.model.Admin;
import com.situ.studentmanagement.model.CheckInfo;
import com.situ.studentmanagement.model.ClassRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by niugao on 6/14/2016.
 */

@Controller
@RequestMapping("/account")
public class AdminAccountController {

    @Autowired
    private AdminMapper adminDao;
    @Autowired
    StudentMapper studentMapper;

    private String errMsg;
    @RequestMapping(value="/sign_up",method = RequestMethod.GET)
    public String signUp(){
        return "sign_up_admin";
    }

    @RequestMapping(value="/man",method = RequestMethod.GET)
    public String logMan(){
        return "main";
    }

//管理员注册
    @RequestMapping(value="/register",method = RequestMethod.POST)
    public String register(HttpSession session,Model model,
                           @RequestParam("name")String name,
                           @RequestParam("password") String password,
                           @RequestParam("phone") String phone,
                           @RequestParam("email") String email,
                           @RequestParam("address") String address)
            throws UnsupportedEncodingException {

        if (name.equals("")) {
            name = null;
        }
        if (password.equals("")) {
            password = null;
        }
        if (phone.equals("")) {
            phone = null;
        }
        if (email.equals("")) {
            email = null;
        }
        if (address.equals("")) {
            address = null;
        }
        if (name == null || password == null ) {
            errMsg = "用户名/密码/性别 不能为空！！！";
            //放到model中的数据可以在jsp中直接使用
            model.addAttribute("errMsg", errMsg);
//                    return "redirect:student/sign_up_student";
            return "sign_up_admin";
        }
        //执行mapper文件中定义的SQL语句
        Admin admin=new Admin(0,name,password,phone,email,address);
        adminDao.register(admin);
        //注册成功，回到登录页面
        session.setAttribute("value_name",name);
        String url= "redirect:/";
        return url;
    }
    @RequestMapping(value="/quxiao")
    public String quxiao(){
        return "index";
    }


}
